
package auto;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class RegistracijaKorisnika extends JDialog {

        /**
         *
         */
        private static final long serialVersionUID = -626367342975556763L;

        Dimension dimtxf = new Dimension(170, 20);

        private JTextField ime = null;
        private JTextField prezime = null;
        private JTextField username = null;
        private JTextField pass = null;
        private JTextField matBr = null;
        private JTextField adr = null;
        private JTextField fon = null;
        private JTextField fon2 = null;
        private JTextField email = null;
        private JCheckBox aktivan = null;
        private JCheckBox zaposlen = null;
        private JComboBox uloga = null;
        private JPanel panel = null;
        private JPanel panel2 = null;
        private JPanel panel3 = null;
        private JLabel ulogaa = null;
        private JButton ok = null;
        private JButton cancel = null;
        private JRadioButton radio1 = null;
        private JRadioButton radio2 = null;
        private ButtonGroup bg = new ButtonGroup();
    
        public ButtonGroup getBg() {
                return bg;
        }




        Dimension d = new Dimension(60,25);
        String[] str = {"Administrator", "Menadzer", "Perac", "Knjigovodja"};
       
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();

        private int hght = 460;
        private int wtdh = 300;

        public RegistracijaKorisnika() {

                setTitle("Unos korisnika");
                init();
                labele();
               

        }
       
        public void init() {
                Toolkit kit = Toolkit.getDefaultToolkit();
                Dimension dim1 = kit.getScreenSize();
               
                setBounds((dim1.width - wtdh) / 2, (dim1.height - hght) / 2, wtdh, hght);
                setContentPane(getPanel());
                setResizable(false);
        }
       
        private void labele(){
                JLabel l1 = new JLabel("Ime: ");
                gbcXY(1, 1);
                panel.add(l1);
               
                gbc.anchor=GridBagConstraints.LINE_START;
                gbl.setConstraints(l1, gbc);
               
                JLabel l2 = new JLabel("Prezime:");
                gbcXY(1, 2);
                panel.add(l2);
                gbl.setConstraints(l2, gbc);
               
                JLabel l3 = new JLabel("Korisnicko:");
                gbcXY(1, 3);
                panel.add(l3);
                gbl.setConstraints(l3, gbc);
               
                JLabel l4 = new JLabel("Password:");
                gbcXY(1, 4);
                panel.add(l4);
                gbl.setConstraints(l4, gbc);
               
                JLabel l5 = new JLabel("Mat.Br.:");
                gbcXY(1, 5);
                panel.add(l5);
                gbl.setConstraints(l5, gbc);
               
                JLabel l6 = new JLabel("Adresa:");
                gbcXY(1, 6);
                panel.add(l6);
                gbl.setConstraints(l6, gbc);
               
                JLabel l7 = new JLabel("Br. Telefona:");
                gbcXY(1, 7);
                panel.add(l7);
                gbl.setConstraints(l7, gbc);
               
                JLabel l8 = new JLabel("Br. Telefona2:");
                gbcXY(1, 8);
                panel.add(l8);
                gbl.setConstraints(l8, gbc);
               
               
               
                JLabel l9 = new JLabel("Email:");
                gbcXY(1, 9);
                panel.add(l9);
                gbl.setConstraints(l9, gbc);
               
                JLabel ulogaa = new JLabel("Uloga:");
                gbcXY(1,10);
                panel.add(ulogaa);
                gbl.setConstraints(ulogaa, gbc);
               
               
                JLabel l10 = new JLabel("Zaposlen:");
                gbcXY(1, 11);
                panel.add(l10);
                gbl.setConstraints(l10, gbc);
               
                JLabel l11 = new JLabel("Aktivan:");
                gbcXY(1, 12);
                panel.add(l11);
                gbl.setConstraints(l11, gbc);
       
               
        }
                private JPanel getPanel(){
                if (panel2 == null){
                        panel2 = new JPanel(new BorderLayout());
                        panel2.add(getPanelData(), BorderLayout.NORTH);
                       
                        panel2.add(getButtonPanel(), BorderLayout.SOUTH);
                }return panel2;
               
        }
        private JPanel getPanelData(){
                if (panel == null){
                        panel = new JPanel(gbl);
                        panel.add(getIme());
                        panel.add(getPrezime());
                        panel.add(getAdress());
                        panel.add(getFon());
                        panel.add(getFon2());
                        panel.add(getPass());
                        panel.add(getKorisnicko());
                        panel.add(getMatBr());
                        panel.add(getAktivan());
                        panel.add(getZaposljen());
                        panel.add(getUlogaCombo());
                        panel.add(getEmail());
                        panel.add(getRadio1());
                        panel.add(getRadio2());
                }return panel;
               
        }

        private JPanel getButtonPanel(){
                if (panel3 == null){
                        FlowLayout fl = new FlowLayout(FlowLayout.CENTER, 25, 5);
                       
                        panel3 = new JPanel(fl);
                        panel3.add(getOkButton());
                        JButton b1 = new JButton("Cancel");
                    	b1.addActionListener(new ActionListener() {
        					
        					@Override
        					public void actionPerformed(ActionEvent e) {
        						dispose();
        						
        					}
        				});
                        b1.setMargin(new Insets(5, 5, 5, 5));
                        b1.setPreferredSize(d);
                        panel3.add(b1);
                       
                }return panel3;
               
        }
       
        private JButton getOkButton(){
                if (ok == null){
                        ok = new JButton("OK");
                        ok.setMargin(new Insets(5, 5, 5, 5));
                        ok.setPreferredSize(d);
                        
                        ok.addActionListener(new ActionListener() {
							
							@Override
							public void actionPerformed(ActionEvent arg0) {
								DodavanjeKorisnikaUBazu.dodaj(uzmiIme(), uzmiPrezime(), uzmiKorisnicko(), uzmiPassword(), uzmiMaticni(), uzmiAdresu(), uzmiFon(), uzmiFon2(), uzmiUlogu(), uzmiZaposljen(), uzmiAktivan(), uzmiSmenuID(), uzmiEmail());
								
							}
						});
                }return ok;
        }
       
        
        
        private JTextField getIme() {
                if (ime == null) {
                        ime = new JTextField();
                        ime.setPreferredSize(dimtxf);
                        gbcXY(2, 1);
                        gbl.setConstraints(ime, gbc);
                }
                return ime;
        }
        
        public String uzmiIme(){
        	String ime = getIme().getText();
        	return ime;
        	
        }
        
        private JTextField getPrezime() {
                if (prezime == null) {
                        prezime = new JTextField();
                        prezime.setPreferredSize(dimtxf);
                        gbcXY(2, 2);
                        gbl.setConstraints(prezime, gbc);
                }
                return prezime;
        }
       
        public String uzmiPrezime(){
        	String prezime = getPrezime().getText();
        	return prezime;
        	
        }
        
        
        private JTextField getKorisnicko() {
                if (username == null) {
                        username = new JTextField();
                        username.setPreferredSize(dimtxf);
                        gbcXY(2, 3);
                        gbl.setConstraints(username, gbc);
                }
                return username;
        }
        
        public String uzmiKorisnicko(){
        	String korisnicko = getKorisnicko().getText();
        	return korisnicko;
        	
        }
        
        private JTextField getPass() {
                if (pass == null) {
                        pass = new JTextField();
                        pass.setPreferredSize(dimtxf);
                        gbcXY(2, 4);
                        gbl.setConstraints(pass, gbc);
                }
                return pass;
        }
       
        public String uzmiPassword(){
        	String password = getPass().getText();
        	return password;
        	
        }
        
        private JTextField getMatBr() {
                if (matBr == null) {
                        matBr = new JTextField();
                        matBr.setPreferredSize(dimtxf);
                        gbcXY(2, 5);
                        gbl.setConstraints(matBr, gbc);
                }
                return matBr;
        }
       
        public String uzmiMaticni(){
        	String maticni = getMatBr().getText();
        	return maticni;
        	
        }
        
        
        
        private JTextField getAdress() {
                if (adr == null) {
                        adr = new JTextField();
                        adr.setPreferredSize(dimtxf);
                        gbcXY(2, 6);
                        gbl.setConstraints(adr, gbc);
                }
                return adr;
        }
       
        public String uzmiAdresu(){
        	String maticni = getAdress().getText();
        	return maticni;
        	
        }
        
        private JTextField getFon() {
                if (fon == null) {
                        fon = new JTextField();
                        fon.setPreferredSize(dimtxf);
                        gbcXY(2, 7);
                        gbl.setConstraints(fon, gbc);
                }
                return fon;
        }
       
        public String uzmiFon(){
        	String fon = getFon().getText();
        	return fon;
        	
        }
        
        private JTextField getFon2() {
                if (fon2 == null) {
                        fon2 = new JTextField();
                        fon2.setPreferredSize(dimtxf);
                        gbcXY(2, 8);
                        gbl.setConstraints(fon2, gbc);
                }
                return fon2;
        }
        
        public String uzmiFon2(){
        	String fon2 = getFon2().getText();
        	return fon2;
        	
        }
        
        private JTextField getEmail(){
                if(email == null){
                        email = new JTextField();
                        email.setPreferredSize(dimtxf);
                        gbcXY(2,9);
                        gbl.setConstraints(email, gbc);
                       
                }
                return email;
               
        }
       
        public String uzmiEmail(){
        	String email = getEmail().getText();
        	return email;
        	
        }
        
        private JComboBox getUlogaCombo(){
                if(uloga == null){
                        uloga = new JComboBox(str);
                        gbcXY(2, 10);
                        gbl.setConstraints(uloga, gbc);
                      
                       
                }return uloga;
               
        }
        
        public int uzmiUlogu(){
        	int uloga = getUlogaCombo().getSelectedIndex();
        	
        	return uloga;
        }
        
        private JCheckBox getAktivan(){
                if (aktivan == null){
                        aktivan = new JCheckBox();
                        gbcXY(2, 11);
                        gbl.setConstraints(aktivan, gbc);
                       
                }return aktivan;
               
        }
        
        public boolean uzmiAktivan(){
        	boolean active = false;
        	if (aktivan.isSelected())
        		active = true;
        	return active;
        	
        }
        
        private JCheckBox getZaposljen(){
                if (zaposlen == null){
                        zaposlen = new JCheckBox();
                        gbcXY(2, 12);
                        gbl.setConstraints(zaposlen, gbc);
                       
                }return zaposlen;
               
        }
        public boolean uzmiZaposljen(){
        	boolean zaposljen = false;
        	if (getZaposljen().isSelected())
        		zaposljen = true;
        	return zaposljen;
        	
        }

        public JRadioButton getRadio1() {
                if (radio1 == null) {
                        radio1 = new JRadioButton("1. Smena");
                        radio1.setSelected(true);
                        bg.add(radio1);
                        gbcXY(1, 13);
                        gbl.setConstraints(radio1, gbc);
                }
                return radio1;
        }

        public JRadioButton getRadio2() {
                if(radio2 == null){
                        radio2 = new JRadioButton("2. Smena");
                        radio2.setSelected(false);
                        bg.add(radio2);
                        gbcXY(2,13);
                        gbl.setConstraints(radio2, gbc);
                }
                return radio2;
        }
        
        public int uzmiSmenuID(){
        	int id;
        	if (getRadio1().isSelected())
        		id =  Var.korisnikID1;
        	else 
        		id = Var.korisnikID2;
        	
        	return id;
        }
       
       
        public void gbcXY(int x, int y) {
                gbc.gridx = x;
                gbc.gridy = y;
                gbc.insets = new Insets(5, 5, 5, 5);
        }

}

